package com.zlp.weixin.config;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;
@Component
@Primary
public class GateWayFilter implements SwaggerResourcesProvider {

	@Value("${zuul.swagger.document}")
	private String document;

	@Override
	public List<SwaggerResource> get() {

	/*
		List resources = new ArrayList<>();
		resources.add(swaggerResource("app-zlp-weixin", "/app-zlp-weixin/v2/api-docs", "2.0")); //v2/api-docs是swagger中的
		resources.add(swaggerResource("app-zlp-member", "/app-zlp-member/v2/api-docs", "2.0"));*/
		// 从阿波罗平台获取配置文件
		JSONArray docJsonArray = JSONArray.parseArray(document);
		List resources = new ArrayList<>();
		// 遍历集合数组
		for (Object object : docJsonArray) {
			JSONObject jsonObject = (JSONObject) object;
			String name = jsonObject.getString("name");
			String location = jsonObject.getString("location");
			String version = jsonObject.getString("version");
			resources.add(swaggerResource(name, location, version));
		}
		return resources;
	}

	private SwaggerResource swaggerResource(String name, String location, String version) {
		SwaggerResource swaggerResource = new SwaggerResource();
		swaggerResource.setName(name);
		swaggerResource.setLocation(location);
		swaggerResource.setSwaggerVersion(version);
		return swaggerResource;
	}


}
